package com.feiyu.paymentAndcredit.controller;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.feiyu.paymentAndcredit.dto.PaymentDto;
import com.feiyu.paymentAndcredit.entity.DealEntity;
import com.feiyu.paymentAndcredit.service.DealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.feiyu.paymentAndcredit.entity.BuyerEntity;
import com.feiyu.paymentAndcredit.service.BuyerService;
import com.feiyu.common.utils.PageUtils;
import com.feiyu.common.utils.R;


/**
 * @author wanghanbin
 * @email 1336891708@qq.com
 * @date 2021-04-03 12:36:05
 */
@RestController
public class BuyerController {
    @Autowired
    private BuyerService buyerService;

    @Autowired
    private DealService dealService;

    /**
     * 租户查看钱包
     */
    @PostMapping("/searchBuyerWallet")
    public R searchBuyerWallet(@RequestBody BuyerEntity buyerEntity) {
        BuyerEntity buyer = buyerService.getById(buyerEntity.getBuyerId());
        if (buyer != null) {
            HashMap<String, Object> hashMap = new HashMap<String, Object>();
            hashMap.put("buyerId", buyer.getBuyerId());
            hashMap.put("userId", buyer.getUserId());
            hashMap.put("buyerMoney", buyer.getBuyerMoney());
            return R.ok("查看成功").put("data", hashMap);
        } else {
            return R.error("查看失败").put("data", null);
        }
    }

    /**
     * 租户充值账户
     */
    @PostMapping("/addBuyerWalletAmount")
    public R addBuyerWalletAmount(@RequestBody BuyerEntity buyerEntity) {
        BuyerEntity buyer = buyerService.getById(buyerEntity.getBuyerId());
        if (buyer == null) {
            return R.error("未查询到该用户").put("data", null);
        }
        float money = buyerEntity.getBuyerMoney();
        float newBuyerMoney = buyer.getBuyerMoney() + money;
        buyerEntity.setBuyerMoney(newBuyerMoney);
        buyerService.updateById(buyerEntity);
        buyer = buyerService.getById(buyerEntity.getBuyerId());
        DealEntity dealEntity = new DealEntity();
        dealEntity.setMainTraderId(buyer.getUserId());
        dealEntity.setMinorTrader("飞鱼商城平台");
        dealEntity.setDealTime(new Date());
        dealEntity.setDealAmount(money);
        dealEntity.setDealIntro("充值账户");
        dealService.save(dealEntity);
        HashMap<String, Object> hashMap = new HashMap<String, Object>();
        hashMap.put("buyerId", buyer.getBuyerId());
        hashMap.put("userId", buyer.getUserId());
        hashMap.put("buyerMoney", buyer.getBuyerMoney());
        return R.ok("操作成功").put("data", hashMap);
    }

    /**
     * 租户查看信用分数
     */
    @PostMapping("/searchBuyerCreditScore")
    public R searchBuyerCreditScore(@RequestBody BuyerEntity buyerEntity) {
        BuyerEntity buyer = buyerService.getById(buyerEntity.getBuyerId());
        if (buyer == null) {
            return R.error("查看失败").put("data", null);
        }
        HashMap<String, Object> hashMap = new HashMap<String, Object>();
        hashMap.put("buyerId", buyer.getBuyerId());
        hashMap.put("userId", buyer.getUserId());
        hashMap.put("buyerCredit", buyer.getBuyerCredit());
        return R.ok("查看成功").put("data", hashMap);
    }

    /**
     * 更新租户信用分数
     */
    @PostMapping("/updateBuyerCreditScore")
    public boolean updateBuyerCreditScore(@RequestBody String orderId) {
        dealService.updateBuyerCreditScore(orderId);
        return true;
    }


}
